package org.NooLab.itexx.app.config;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;



import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.CreateLogging;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.StringsUtil;



/**
 * 
 * 
 * 
 *
 */
public class CrawlerSettings 
								implements 
											CrawlerConfigIntf{

	// constants ......................
	                                          
	public final static String REGEX_IMAGES = ".*(\\.(gif|jpg|jpeg|bmp|tif|tiff|png))$" ;
	public final static String REGEX_BINARY = ".*(\\.(zip|rar|gz))$" ;
	public final static String REGEX_SOUND  = ".*(\\.(mp2|mp3|mp4|wav|avi|mov|mpeg|m4v|rm|wmv|wma))$" ;
	public final static String REGEX_DOC    = ".*(\\.(doc|pdf|html?|txt|php|pl))$" ; 
	public final static String _SEARCH_GOOGLE = "google" ;
	public final static String _SEARCH_BING   = "bing" ;
	public final static String _SEARCH_YAHOO  = "yahoo" ;
	
	String configFilePath="";
	
	/** 0 = no saving, direct on-line processing (such as indexing);
	 *  1 = complete html content, 2 = only parsed text, without post-proc, 3 = 2+post-processing  (acc to xml, links are separated into an appendix) */
	int docsavingmode = 3 ;
	
	String downloadHomePath = "C:/temp/~nc.tmp/";
	String downloadPath = "sites/"; // "sites/[domain]";

	
	// main variables / properties ....
	
	int inSiteMaxDocCount = 20 ;
	private int totalMaxDocCount = 20 ;
	
	int inSiteDepth = 3 ;
	int outSitemaxHops = 1 ;
	
	/**
	 * ???
	 */
	boolean preferOutLinksinText = true ;
	boolean denyOutLinksbyImage  = true ; // does not apply, if we are searching for images !!
	/**
	 * delayed handling of links from menu's, that is, links from menu's are treated as last ones
	 */
	boolean deferOutLinksinMenu  = true ;
	
	/** 0=no images; 1=just images; 2=images, indexed together/linked to textual web-som  */
	int includeImages = 0;
	boolean onlyImages = false;
	
	/**  if we save just the text, we insert meta tags using the format {@ ...} ;<br/>
	 *   this allows for better indexing of the texts
	 */
	boolean extraTagsforSavedXDocs = true;
	
	/** trying to get rid of ads, irrelevant links like 
	 *  google cache, google translator, google command bar ... 
	 */
	boolean specificDomainPostProcessing = false;
	
	
	boolean allowClimbingUp = false;
	boolean belowSeedUrlonly = true; 
	
	/**
	 * for requests to the same server, we use a delay, which additionally gets randomly varied, from min100 to 400...
	 */
	int politenessDelay = 250;  

	boolean isSearchEngine = false;
	String defaultSearchEngine = _SEARCH_GOOGLE;
	
	public Map<String,String> regexPatterns = new HashMap<String,String>();
	

	int maxConcurrentProcesses = 4;
	
	Properties prop = new Properties();
	
	

	CreateLogging logging = new CreateLogging();
	

	
	
	int printLevel = 3;
	PrintLog out ;
	
	StringsUtil strgutil = new StringsUtil();
	DFutils filutils = new DFutils();
	
	// ========================================================================
	public CrawlerSettings(){

	}

	// for cloning...
	public CrawlerSettings(CrawlerConfigIntf inCfg) {
		
		maxConcurrentProcesses = inCfg.getMaxConcurrentProcesses()   ;

		boolean isSearchEngine = false;
		String defaultSearchEngine = _SEARCH_GOOGLE;
		
		inSiteDepth = inCfg.getInSiteDepth()   ;
		inSiteMaxDocCount = inCfg.getInSiteMaxDocCount()   ;
		
		outSitemaxHops = inCfg.getOutSitemaxHops()   ;
		
		
		preferOutLinksinText = inCfg.isPreferOutLinksinText()   ;
		denyOutLinksbyImage = inCfg.isDenyOutLinksbyImage()   ;
		deferOutLinksinMenu = inCfg.isDeferOutLinksinMenu()   ;
		
		includeImages = inCfg.getIncludeImages()   ;
		onlyImages = inCfg.isOnlyImages()   ;
		
		extraTagsforSavedXDocs = inCfg.isExtraTagsforSavedXDocs()   ;
		
		specificDomainPostProcessing = inCfg.isSpecificDomainPostProcessing()   ;
		
		docsavingmode = inCfg.getDocsavingmode()   ;
		
		downloadHomePath = inCfg.getDownloadHomePath()   ;
		downloadPath = inCfg.getDownloadPath()   ;
		
		allowClimbingUp = inCfg.isAllowClimbingUp()   ;
		belowSeedUrlonly = inCfg.isBelowSeedUrlonly()   ;
		
		politenessDelay = inCfg.getPolitenessDelay()   ;

		isSearchEngine = inCfg.isSearchEngine();
		defaultSearchEngine = inCfg.getDefaultSearchEngine() ;
			
		totalMaxDocCount = inCfg.getTotalMaxDocCount() ;
		

		printLevel = inCfg.getPrintLevel()   ;

		
	}
	// ========================================================================

	
	// ------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#load()
	 */
	@Override
	public void load(){

		if (filutils.fileexists(configFilePath)){
			// load it
		}

	}

	
	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getCompleteDownloadPath(java.lang.String)
	 */
	@Override
	public String getCompleteDownloadPath( String pathsnip ){
		String str, filpath;
		
		str = downloadHomePath +"/"+ downloadPath +"/" + pathsnip;
		filpath = strgutil.replaceAll(str, "//", "/") ;
		
		File fil = new File(filpath);
		
		if (fil.exists()==false){
			fil.mkdirs() ;	
		}
		
		return str ;
	}
	 
	
	// ------------------------------------------------------------------------

	
	@Override
	public String getConfigFilePath() {
		return configFilePath;
	}

	@Override
	public void setConfigFilePath(String configFilePath) {
		this.configFilePath = configFilePath;
	}

	@Override
	public int getInSiteDepth() {
		return inSiteDepth;
	}

	@Override
	public void setInSiteDepth(int inSiteDepth) {
		this.inSiteDepth = inSiteDepth;
	}

	@Override
	public int getInSiteMaxDocCount() {
		return inSiteMaxDocCount;
	}

	@Override
	public void setInSiteMaxDocCount(int inSiteMaxDocCount) {
		this.inSiteMaxDocCount = inSiteMaxDocCount;
	}

	@Override
	public int getOutSitemaxHops() {
		return outSitemaxHops;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setOutSitemaxHops(int)
	 */
	
	@Override
	public void setOutSitemaxHops(int outSitemaxHops) {
		this.outSitemaxHops = outSitemaxHops;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isSpecificDomainPostProcessing()
	 */
	
	@Override
	public boolean isSpecificDomainPostProcessing() {
		return specificDomainPostProcessing;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setSpecificDomainPostProcessing(boolean)
	 */
	
	@Override
	public void setSpecificDomainPostProcessing(boolean specificDomainPostProcessing) {
		this.specificDomainPostProcessing = specificDomainPostProcessing;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getDocsavingmode()
	 */
	
	@Override
	public int getDocsavingmode() {
		return docsavingmode;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setDocsavingmode(int)
	 */
	
	@Override
	public void setDocsavingmode(int docsavingmode) {
		this.docsavingmode = docsavingmode;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isAllowClimbingUp()
	 */

	@Override
	public boolean isAllowClimbingUp() {
		return allowClimbingUp;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setAllowClimbingUp(boolean)
	 */
	@Override
	public void setAllowClimbingUp(boolean allowClimbingUp) {
		this.allowClimbingUp = allowClimbingUp;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setDownloadsOnlyBelowPath(boolean)
	 */
	@Override
	public void setDownloadsOnlyBelowPath( boolean onlybelow){
		belowSeedUrlonly = onlybelow ;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getDownloadsOnlyBelowPath()
	 */
	@Override
	public boolean getDownloadsOnlyBelowPath(){
		return belowSeedUrlonly  ;
	}
	
	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getPolitenessDelay()
	 */
	@Override
	public int getPolitenessDelay() {
		return politenessDelay;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setPolitenessDelay(int)
	 */
	@Override
	public void setPolitenessDelay(int politenessDelay) {
		this.politenessDelay = politenessDelay;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getMaxConcurrentProcesses()
	 */
	@Override
	public int getMaxConcurrentProcesses() {
		return maxConcurrentProcesses;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setMaxConcurrentProcesses(int)
	 */
	@Override
	public void setMaxConcurrentProcesses(int maxConcurrentProcesses) {
		this.maxConcurrentProcesses = maxConcurrentProcesses;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getOut()
	 */
	@Override
	public PrintLog getOut() {
		return out;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setOut(org.NooLab.utilities.logging.PrintLog)
	 */
	@Override
	public void setOut(PrintLog out) {
		this.out = out;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getPrintLevel()
	 */

	@Override
	public int getPrintLevel() {
		return printLevel;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setPrintLevel(int)
	 */

	@Override
	public void setPrintLevel(int printlevel) {
		this.printLevel = printlevel;
		
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getLogging()
	 */
	@Override
	public CreateLogging getLogging() {
		return logging;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isDenyOutLinksbyImage()
	 */
	@Override
	public boolean isDenyOutLinksbyImage() {
		return denyOutLinksbyImage;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setDenyOutLinksbyImage(boolean)
	 */
	@Override
	public void setDenyOutLinksbyImage(boolean denyOutLinksbyImage) {
		this.denyOutLinksbyImage = denyOutLinksbyImage;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isDeferOutLinksinMenu()
	 */
	@Override
	public boolean isDeferOutLinksinMenu() {
		return deferOutLinksinMenu;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setDeferOutLinksinMenu(boolean)
	 */
	@Override
	public void setDeferOutLinksinMenu(boolean deferOutLinksinMenu) {
		this.deferOutLinksinMenu = deferOutLinksinMenu;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getIncludeImages()
	 */
	@Override
	public int getIncludeImages() {
		return includeImages;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setIncludeImages(int)
	 */
	@Override
	public void setIncludeImages(int includeImages) {
		this.includeImages = includeImages;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isOnlyImages()
	 */
	@Override
	public boolean isOnlyImages() {
		return onlyImages;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setOnlyImages(boolean)
	 */
	@Override
	public void setOnlyImages(boolean onlyImages) {
		this.onlyImages = onlyImages;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getDownloadHomePath()
	 */
	@Override
	public String getDownloadHomePath() {
		return downloadHomePath;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setDownloadHomePath(java.lang.String)
	 */
	@Override
	public void setDownloadHomePath(String downloadHomePath) {
		this.downloadHomePath = downloadHomePath;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getDownloadPath()
	 */
	@Override
	public String getDownloadPath() {
		return downloadPath;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setDownloadPath(java.lang.String)
	 */
	@Override
	public void setDownloadPath(String downloadPath) {
		this.downloadPath = downloadPath;
	}


	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isPreferOutLinksinText()
	 */
	@Override
	public boolean isPreferOutLinksinText() {
		return preferOutLinksinText;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setPreferOutLinksinText(boolean)
	 */
	@Override
	public void setPreferOutLinksinText(boolean preferOutLinksinText) {
		this.preferOutLinksinText = preferOutLinksinText;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isExtraTagsforSavedXDocs()
	 */
	@Override
	public boolean isExtraTagsforSavedXDocs() {
		return extraTagsforSavedXDocs;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setExtraTagsforSavedXDocs(boolean)
	 */
	@Override
	public void setExtraTagsforSavedXDocs(boolean extraTagsforSavedXDocs) {
		this.extraTagsforSavedXDocs = extraTagsforSavedXDocs;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#isBelowSeedUrlonly()
	 */
	@Override
	public boolean isBelowSeedUrlonly() {
		return belowSeedUrlonly;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setBelowSeedUrlonly(boolean)
	 */
	@Override
	public void setBelowSeedUrlonly(boolean belowSeedUrlonly) {
		this.belowSeedUrlonly = belowSeedUrlonly;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getRegexPatterns()
	 */
	@Override
	public Map<String, String> getRegexPatterns() {
		return regexPatterns;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setRegexPatterns(java.util.Map)
	 */
	@Override
	public void setRegexPatterns(Map<String, String> regexPatterns) {
		this.regexPatterns = regexPatterns;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getProp()
	 */
	@Override
	public Properties getProp() {
		return prop;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setProp(java.util.Properties)
	 */
	@Override
	public void setProp(Properties prop) {
		this.prop = prop;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getFilutils()
	 */
	@Override
	public DFutils getFilutils() {
		return filutils;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setFilutils(org.NooLab.utilities.files.DFutils)
	 */
	@Override
	public void setFilutils(DFutils filutils) {
		this.filutils = filutils;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#getStrgutil()
	 */
	@Override
	public StringsUtil getStrgutil() {
		return strgutil;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setStrgutil(org.NooLab.utilities.strings.StringsUtil)
	 */
	@Override
	public void setStrgutil(StringsUtil strgutil) {
		this.strgutil = strgutil;
	}

	/* (non-Javadoc)
	 * @see org.NooLab.retriever.config.ttt#setLogging(org.NooLab.utilities.logging.CreateLogging)
	 */
	@Override
	public void setLogging(CreateLogging logging) {
		this.logging = logging;
	}

	public boolean isSearchEngine() {
		return isSearchEngine;
	}

	public void setSearchEngine(boolean isSearchEngine) {
		this.isSearchEngine = isSearchEngine;
	}

	public String getDefaultSearchEngine() {
		return defaultSearchEngine;
	}

	public void setDefaultSearchEngine(String defaultSearchEngine) {
		this.defaultSearchEngine = defaultSearchEngine;
	}

	@Override
	public int getTotalMaxDocCount() {
		
		return totalMaxDocCount;
	}

	@Override
	public void setTotalMaxDocCount(int docCount) {
		// 
		totalMaxDocCount = docCount;
	}

}
